package com.honeyms.project.system.user.controller;

import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honeyms.common.constant.ShiroConstants;
import com.honeyms.common.utils.R;
import com.honeyms.common.utils.ServletUtils;
import com.honeyms.framework.web.controller.BaseController;
import com.honeyms.project.im.email.service.EmailService;
import com.honeyms.project.system.config.service.IConfigService;
import com.honeyms.project.system.user.domain.User;
import com.honeyms.project.system.user.service.IUserService;

@Controller
@RequestMapping("/findpwd")
public class FindpwdController extends BaseController{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private EmailService emailService;

    @Autowired
    private IConfigService configService;
	
	@GetMapping("/findpwd")
	public String findpwd() {
		return "findpwd";
	}
	
	@PostMapping("/findpwd")
	@ResponseBody
	public R ajaxfindpwd(String loginname,String password,String vcode,String validateCode,HttpServletRequest request) {
		System.out.println(loginname);
		System.out.println(password);
		System.out.println(vcode);
		if (!StringUtils.isEmpty(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA)))
        {
			return error(1,"验证码错误");
        }
		if (request.getSession().getAttribute(loginname)==null) {
			logger.info("找回密码： {} => 找回密码失败!  原因:当前找回的账号没有经过验证或验证码失效!", loginname);
			return error(1,"你没有获取验证码或验证码已经过期请从新获取!");
		}
		if (!request.getSession().getAttribute(loginname).equals(vcode)) {
			logger.info("找回密码： {} => 找回密码失败!  原因:验证码不对!", loginname);
			return error(1,"邮箱验证码错误");
		}
		User user = userService.selectUserByLoginName(loginname);
		user.setPassword(password);
		if (userService.resetUserPwd(user) > 0) {
			return ok(0,"找回成功");
		}
		return error(1,"修改失败,原因未知");
	}
	
	@GetMapping("/findpwdVerify")
	@ResponseBody
	public R findpwdVerify(String loginName,HttpServletRequest request) {
		HttpSession session = request.getSession();
		User user = userService.selectUserByLoginName(loginName);
		if (user==null) {
			logger.info("获取验证码： {} => 获取验证码失败!  原因:此账号不存在或绑定邮箱不对", loginName);
			return error(1,"获取验证码失败,请检查你的账号!");
		}
		if (session.getAttribute(loginName)!=null) {
			return error(1,"请五分钟后再次获取!");
		}
		//获取验证码
		String random = String.valueOf((int)((Math.random()*9+1)*1000));
		if(!"true".equals(configService.selectConfigByKey("sys.blog.email"))) {
			return error(1,"站长暂未开发找回密码功能!");
		}
		String  emailtext="好呢博客用户:<br>&nbsp;你获取的验证码为:<a>"+random+"</a>;请勿泄露!泄露将导致账号被盗!本验证码5分钟内有效,请及时使用!";
		session.setAttribute(loginName, random);
		emailService.sendhtmlemail(user.getEmail(), emailtext);
		System.out.println(random);
		final Timer timer=new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				session.removeAttribute(loginName);
			  	System.out.println("checkCode删除成功");
			  	timer.cancel();
			}
		},2*60*1000);
		return ok(0,"验证码已发送到你的邮箱,2分钟内有效!");
	}
	
}
